package com.romain.risotto.entities;

import static org.andengine.extension.physics.box2d.util.constants.PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT;

import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.AlphaModifier;
import org.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.andengine.entity.modifier.ParallelEntityModifier;
import org.andengine.entity.modifier.ScaleModifier;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.util.math.MathUtils;
import org.andengine.util.modifier.IModifier;

import android.util.Log;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.romain.risotto.Constantes;
import com.romain.risotto.manager.GameManager;
import com.romain.risotto.texture.Textures;

public class Ship extends SpaceSpriteObject  {

	private boolean isGazOn = false;
	private final AnimatedSprite gazSprite;
	private final Sprite explosionSprite;
	private int life = 2;
	private boolean isMainShip=false;
	private boolean isDead = false;
	public float bulletX = 0;
	public float bulletY = 0;
	

	public Ship(boolean pisAutoUpdated, float pX, float pY,
			ITextureRegion pTextureRegion, VertexBufferObjectManager vbo,boolean pisMain) {
		super(pisAutoUpdated, pX, pY, pTextureRegion, vbo);
		isMainShip = pisMain;
		createBody();
		game.physicWorld.registerPhysicsConnector(new PhysicsConnector(this, body, true, true));
		explosionSprite = new Sprite(0, 0, Textures.textureExplosion, vbo);
		bulletX = this.getWidthScaled()/2;
		bulletY = -1;
		
		gazSprite = new AnimatedSprite(
				(Textures.textureShip.getWidth() - Textures.textureGaz
						.getWidth()) / 2,
						Textures.textureShip.getHeight() - 4, Textures.textureGaz, this
						.getVertexBufferObjectManager());
		gazSprite.animate(20);
		gazSprite.setVisible(false);
		this.attachChild(gazSprite);
	}

	public void setGaz(boolean value) {
		if (isDead) {
			return;
		}
		isGazOn = value;
		gazSprite.setVisible(value);
	}
	@Override
	public void setRotation(float pRotation) {
		this.direction = MathUtils.degToRad(pRotation);
		super.setRotation(pRotation);
	}
	@Override
	protected void onManagedUpdate(final float pSecondsElapsed) {
		if(isManualUpdated)
			return;
		if (!isDead) {
			if (isGazOn) {
				
				float accX =(float) (Constantes.SHIP_ACCELERATION_COEFF *Math.cos(direction - Math.PI / 2));
				float accY =(float) (Constantes.SHIP_ACCELERATION_COEFF *Math.sin(direction - Math.PI / 2));
				
				Vector2 currentVitesse = body.getLinearVelocity();
				if(Math.abs(currentVitesse.x+pSecondsElapsed*accX)>Constantes.SHIP_MAX_SPEED)
				{
					accX=0;
				}
				if(Math.abs(currentVitesse.y+pSecondsElapsed*accY)>Constantes.SHIP_MAX_SPEED)
				{
					accY=0;
				}
				body.applyForceToCenter(new Vector2(accX,accY));
				
				
			}
			else{
			}
			
		}
		
		super.onManagedUpdate(pSecondsElapsed);
		GameManager.getInstance().updateMainShip(this);
		
	}

	public void touch(Bullet bullet) {
		life -= bullet.getDamageValue();
		if (life <= 0) {
			this.explode();
		}
		Log.d("risotto", "touched !!!  " + life + " remaining..");
	}

	public void explode() {
		if (isDead) {
			return;
		}
		isDead = true;
		life = 0;
		gazSprite.setVisible(false);
		isGazOn = false;
		//physic.setVelocity(0,0);

		ParallelEntityModifier sm = new ParallelEntityModifier(
				new ScaleModifier(0.6f, 0.2f, 1.7f), new AlphaModifier(1.6f, 1,
						0));
		sm.addModifierListener(new IEntityModifierListener() {
			@Override
			public void onModifierStarted(final IModifier<IEntity> pModifier,
					final IEntity pItem) {
			}

			@Override
			public void onModifierFinished(
					final IModifier<IEntity> pEntityModifier,
					final IEntity pEntity) {
				Ship.this.delete();
			}
		});

		this.setAlpha(0);
		Log.d("risotto", "explostion du ship id: "+id);
		GameManager.getInstance().registerDead(this);
		explosionSprite.setScale(0.2f);
		explosionSprite.setPosition(-10, -10);
		explosionSprite.registerEntityModifier(sm);
		this.attachChild(explosionSprite);

	}

	public boolean isDead() {
		return isDead;
	}
	
	protected void createBody() {
		final float halfWidth = this.getWidthScaled() * 0.5f / PIXEL_TO_METER_RATIO_DEFAULT;
		final float halfHeight = this.getHeightScaled() * 0.5f / PIXEL_TO_METER_RATIO_DEFAULT;


		//final float p2 = halfWidth - 2.5f / PIXEL_TO_METER_RATIO_DEFAULT;
		Vector2 v1 = new Vector2(0, -halfHeight);
		Vector2 v2 = new Vector2(halfWidth, (float) (halfHeight*.4));
		Vector2 v3 = new Vector2(0, halfHeight);
		Vector2 v4 = new Vector2(-halfWidth, (float) (halfHeight*.4));

		// attention au sens horaire pour les contours
		final Vector2[] vertices = {
				v1,v2,v3,v4
		};
		final FixtureDef shipFixture ;
		if(isMainShip){
			shipFixture = GameManager.FIXTURE_MY_SHIP_DEF;
		}
		else{
			shipFixture = GameManager.FIXTURE_OTHER_SHIP_DEF;
		}
		body = PhysicsFactory.createPolygonBody(game.physicWorld, this, vertices, BodyType.DynamicBody, shipFixture);
		
	}

	

}
